% linea.m

% Stima i parametri di una linea e gli intervalli di confidenza sui 
% parametri

% a11 x1 + a12 x2 = b
% x1 = -a12 / a11 x2 + b / a11     a11 = 1   -a12 / a11 = m    q = b / a11
%  y = mx + q

clear; format compact; format long;
for i=1:10, figure(i); delete(i); end;

seed = 1
rng(seed);
rumore = 0.05

N = 10000;

m = 1
q = 2

% RandStream.setDefaultStream('seed',1);

x2 = randn(N,1)
x1 = x2 .*m + q

r = randn(N,1)*rumore
x1 = x1 + r; 

figure(1); hold on;
plot(x2,x1,'*r');

A = [x2, ones(N,1)]
bv = x1

X =inv(A'*A)*A'*bv


xr(1) = -1;
yr(1) = X(1)*xr(1)+X(2)
xr(2) = 1;
yr(2) = X(1)*xr(2)+X(2)
plot(xr,yr,'-b');

C = inv(A'*A)
m_stimato = X(1)
q_stimato = X(2)

sigma_m_stimato = C(1,1)*rumore*1
sigma_q_stimato = C(2,2)*rumore*1

